<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <title>引出生命周期</title>
</head>
<body>
<div id="app"></div>
<script src="../lib/babel.min.js"></script>
<script src="../lib/react.development.js"></script>
<script src="../lib/react-dom.development.js"></script>
<script type="text/babel">
  class Demo extends React.Component {

    state = {
      opacity: 1.0
    }

    // 组件挂在完毕后调用
    componentDidMount() {
      this.timer = setInterval(() => {
        let {opacity} = this.state;
        opacity -= 0.1
        if (opacity <= 0) {
          opacity = 1.0
        }
        this.setState({
          opacity
        })
      },  200)
    }

    // 组件将要卸载
    componentWillUnmount() {
      clearInterval(this.timer)
    }

    // 销毁组件
    death = () => {
      ReactDOM.unmountComponentAtNode(document.getElementById('app'))
    }

    render() {
      return (
        <div>
          <h1 style={{opacity: this.state.opacity}}>React学不会怎么办</h1>
          <button onClick={this.death}>不活了</button>
        </div>
      )
    }
  }

  ReactDOM.render(<Demo/>, document.getElementById('app'));
</script>
</body>
</html>
